import { ApiExtraModel, ApiProperty, getSchemaPath } from '@midwayjs/swagger';
import { Rule, RuleType, getSchema } from '@midwayjs/validate';
import { Column, Entity, PrimaryColumn } from 'typeorm';

export enum BillState {
  toSubmit = 'toSubmit',
  toAudit = 'toAudit',
  auditted = 'auditted',
}
// 后端模型可以使用 orm方式 关联关系 typorm
// itemId	billFk	priority	goodsCode	goodsName	price	quantity
// 尽量给实体类的属性赋默认值
// string => '' | null ;
// number =>  0;
// Date => null;
// Array => []
@Entity({
  name: 'PurchaseItem',
  synchronize: false,
})
export class PurchaseItem {
  @ApiProperty({ example: 0 })
  @Rule(RuleType.number().required())
  @PrimaryColumn()
  itemId: number = 0;
  @ApiProperty({ example: '' })
  @Rule(RuleType.string().allow('').allow(null).required())
  @PrimaryColumn()
  billFk: string = '';
  @ApiProperty({ example: 0 })
  @Rule(RuleType.number().required())
  @Column()
  priority: number = 0;
  @ApiProperty({ example: '' })
  @Rule(RuleType.string().allow('').allow(null).required())
  @Column({ nullable: true })
  goodsCode: string = '';
  @ApiProperty({ example: '' })
  @Rule(RuleType.string().allow('').allow(null).required())
  @Column({ nullable: true })
  goodsName: string = '';
  @ApiProperty({ example: 0 })
  @Rule(RuleType.number().min(0).required())
  @Column({
    type: 'decimal',
    precision: 18,
    scale: 2,
    transformer: {
      to(data: number): number {
        return data;
      },
      from(data: string): number {
        return parseFloat(data);
      },
    },
  })
  price: number = 0.0;
  @ApiProperty({ example: 0 })
  @Rule(RuleType.number().min(0).required())
  @Column()
  quantity: number = 0;
}
// 主表
@Entity({
  name: 'PurchaseBill',
  synchronize: false,
})
@ApiExtraModel(PurchaseItem)
export class PurchaseBill {
  // billId主键
  @ApiProperty({ example: '' })
  @Rule(RuleType.string().allow('').allow(null).required())
  @PrimaryColumn()
  billId: string = '';
  @ApiProperty({ example: BillState.toSubmit })
  @Rule(
    RuleType.string()
      .valid(...Object.values(BillState))
      .required()
  )
  @Column({
    type: 'enum',
    enum: BillState,
    default: BillState.toSubmit,
  })
  billState: BillState = BillState.toSubmit;
  @ApiProperty({ example: '牛顿实验室-SC009' })
  @Rule(RuleType.string().allow('').allow(null).required())
  @Column({ nullable: true })
  supplier: string = '';
  @ApiProperty({ example: new Date() })
  @Rule(RuleType.date().allow(null).required())
  @Column({ nullable: true })
  orderDate: Date = null;
  @ApiProperty({ example: new Date() })
  @Rule(RuleType.date().allow(null).required())
  @Column({ nullable: true })
  receiveDate: Date = null;
  @ApiProperty({ example: 0 })
  @Rule(RuleType.number().required())
  @Column({
    type: 'decimal',
    precision: 18,
    scale: 2,
    transformer: {
      to(data: number): number {
        return data;
      },
      from(data: string): number {
        return parseFloat(data);
      },
    },
  })
  // 18.10
  amount: number = 0.0;
  @ApiProperty({ example: 0 })
  @Rule(RuleType.number().required())
  @Column()
  nextItemId: number = 0;
  @ApiProperty({ example: '' })
  @Rule(RuleType.string().allow('').allow(null).required())
  @Column({ nullable: true })
  applier: string = '';
  @ApiProperty({ example: new Date() })
  @Rule(RuleType.date().allow(null).required())
  @Column({ nullable: true })
  applyTime: Date = null;
  @ApiProperty({ example: '' })
  @Rule(RuleType.string().allow('').allow(null).required())
  @Column({ nullable: true })
  auditor: string = '';
  @ApiProperty({ example: new Date() })
  @Rule(RuleType.date().allow(null).required())
  @Column({ nullable: true })
  auditTime: Date = null;
  @ApiProperty({ example: '' })
  @Rule(RuleType.string().allow('').allow(null).required())
  @Column({ nullable: true })
  auditMsg: string = '';
  @ApiProperty({ example: '' })
  @Rule(RuleType.string().allow('').allow(null).required())
  @Column({ nullable: true })
  history: string = '';
  @ApiProperty({ example: new Date() })
  @Rule(RuleType.date().allow(null).required())
  @Column({ nullable: true })
  updateTime: Date = null;

  /* 
  "items": [
    {
      "itemId": 0,
      "billFk": "",
      "priority": 0,
      "goodsCode": "",
      "goodsName": "",
      "price": 0,
      "quantity": 0
    }
  ]
  */
  // @ApiProperty({
  //   example: [new PurchaseItem()],
  // })
  @ApiProperty({
    type: 'array',
    items: {
      $ref: getSchemaPath(PurchaseItem),
    },
  })
  @Rule(RuleType.array().items(getSchema(PurchaseItem)).required())
  // @Rule(RuleType.array().items(getSchema(PurchaseItem)).required())
  // 不加column注解 typeorm 就当这一列数据库中不存在
  items: Array<PurchaseItem> = [];
}
// query condition
export class ApplierPurchaseQC {
  @ApiProperty({ example: 0 })
  @Rule(RuleType.number().required())
  minAmount: number = 0;
  @ApiProperty({ example: 0 })
  @Rule(RuleType.number().required())
  maxAmount: number = 0;
  @ApiProperty({ example: '' })
  @Rule(RuleType.string().allow('').allow(null).required())
  nameLike: string = '';
  @ApiProperty({ example: 1 })
  @Rule(RuleType.number().min(1).required())
  pageNum: number;
  @ApiProperty({ example: 3 })
  @Rule(RuleType.number().min(1).required())
  pageSize: number;
}
export class AuditorPurchaseQC {
  @ApiProperty({ example: '' })
  @Rule(RuleType.string().allow('').allow(null).required())
  supplierLike: '';
  @ApiProperty({ example: BillState.toAudit })
  @Rule(
    RuleType.string()
      .valid(...Object.values(BillState))
      .required()
  )
  // 这次不使用 数组方式 而使用单值
  billState: BillState;
  @ApiProperty({ example: 1 })
  @Rule(RuleType.number().min(1).required())
  pageNum: number;
  @ApiProperty({ example: 3 })
  @Rule(RuleType.number().min(1).required())
  pageSize: number;
}
